﻿// ------------------------------------------------------------------------------------------------
//  <copyright file="HtmlContent.cs" company="Iveely">
//    Copyright (c) Iveely Liu.  All rights reserved.
//  </copyright>
//  
//  <Create Time>
//    03/02/2013 21:59 
//  </Create Time>
//  
//  <contact owner>
//    liufanping@iveely.com 
//  </contact owner>
//  -----------------------------------------------------------------------------------------------

#region

using System;
using System.IO;
using System.Net;
using System.Text;
using HtmlAgilityPack;

#endregion

namespace IveelySE.Spider
{
    public class HtmlContent : BasicContent
    {
        public HtmlContent(Uri requestUrl, Uri referrerUrl, HttpWebResponse webResponse)
            : base(requestUrl, referrerUrl, webResponse)
        {
            Html = new HtmlDocument();
            Encoding en = webResponse.CharacterSet.Contains("ISO")
                              ? Encoding.UTF8
                              : Encoding.GetEncoding(webResponse.CharacterSet);
            //Html.Load(Stream, AutoDetectEncoding);
            Html.Load(Stream, en);
        }

        public static bool AutoDetectEncoding { get; set; }

        public HtmlDocument Html { get; protected set; }

        public override string Text
        {
            get { return Html.DocumentNode.OuterHtml; }
        }

        public override void Save(Stream stream)
        {
            Html.Save(stream);
        }

        /// <summary>
        ///   获取网页标题
        /// </summary>
        /// <returns> 返回标题内容 </returns>
        public override string GetTitle()
        {
            try
            {
                return Html.DocumentNode.SelectNodes("/html/head/title")[0].InnerText;
            }
            catch
            {
                return "";
            }
        }

        /// <summary>
        ///   获取网页正文内容
        /// </summary>
        /// <returns> 返回网页正文 </returns>
        public override string GetContent()
        {
            try
            {
                string content = Html.DocumentNode.SelectNodes("/html/body")[0].InnerHtml;
                return new HtmlParser(content).Text();
            }
            catch
            {
                return "";
            }
        }
    }
}